let goBack = {};

goBack.STATE = 'goback'
goBack.element

goBack.onPopState = function (event) {
    event.state === goBack.STATE && goBack.fire()
    goBack.record(goBack.STATE)
}

goBack.record = function (state) {
    console.log(state, location.href)
    history.pushState(state, null, location.href)
}

goBack.fire = function () {
    var event = document.createEvent('Events')
    event.initEvent(goBack.STATE, false, false)
    goBack.element.dispatchEvent(event)
}

goBack.listen = function (listener) {
    goBack.element.addEventListener(goBack.STATE, listener, false)
}

goBack.init = function () {
    goBack.element = document.createElement('span')
    window.addEventListener('popstate', goBack.onPopState)
    goBack.record(goBack.STATE)
}

goBack.remove = function () {
    window.removeEventListener('popstate', goBack.onPopState)
}

export default goBack